package com.pro.springbootshop.service.impl;

import com.pro.springbootshop.entity.Cart;
import com.pro.springbootshop.entity.Wares;
import com.pro.springbootshop.mapper.CartMapper;
import com.pro.springbootshop.mapper.WaresMapper;
import com.pro.springbootshop.service.CartService;
import com.pro.springbootshop.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    TokenUtil tokenUtil;
    public CartMapper cartMapper;
    @Autowired
    public void setUserMapper (CartMapper cartMapper) {
        this.cartMapper = cartMapper;
    }
    public WaresMapper waresMapper;
    @Autowired
    public void setUserMapper (WaresMapper waresMapper) {
        this.waresMapper = waresMapper;
    }
    @Override
    public String addOrUpdateCart(Integer waresId, Integer waresNumber, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        Cart cart=new Cart();
        cart.setCartState(1);
        cart.setWaresId(waresId);
        Wares wares=waresMapper.selectById(waresId);
        if(wares.getWaresNumber()<=waresNumber){
            cart.setWaresNumber(wares.getWaresNumber());
        }else{
            cart.setWaresNumber(waresNumber);
        }
        cart.setBuyerId(buyerId);
        Cart newCart=cartMapper.selectCartById(waresId,buyerId);
        if(newCart==null){
            int i=cartMapper.insert(cart);
            if(i>0){
                return "插入成功";
            }else{
                return "插入失败";
            }
        }else {
            cart.setCartId(newCart.getCartId());
            if(wares.getWaresNumber()<newCart.getWaresNumber()+waresNumber){
                cart.setWaresNumber(wares.getWaresNumber());
            }else{
                cart.setWaresNumber(newCart.getWaresNumber()+waresNumber);
            }
            int i=cartMapper.updateById(cart);
            if(i>0){
                return "修改成功";
            }else{
                return "修改失败";
            }
        }
    }

    @Override
    public List<Cart> getCartList(String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        List<Cart> cartList= cartMapper.selectCartShop(buyerId);
        for(Cart cart:cartList){
            Wares wares=cart.getWares();

            wares.setPictureList(wares.getWaresPicture().split(";"));
        }
        return cartList;
    }

    @Override
    public String deleteCart(Integer cartId) {
        int i=cartMapper.deleteById(cartId);
        if(i>0){
            return "删除成功";
        }else{
            return "删除失败";
        }
    }

    @Override
    public String updateCartChecked(Integer waresId, Integer cartState, String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        Cart cart=cartMapper.selectCartById(waresId,buyerId);
        cart.setCartState(cartState);
        int i=cartMapper.updateById(cart);
        if(i>0){
            return "修改成功";
        }else{
            return "修改失败";
        }
    }

    @Override
    public String clearCart(String token) {
        Map<String,String> buyerMap=tokenUtil.parseToken(token);
        String buyerId=buyerMap.get("buyerId");
        List<Cart> cartList=cartMapper.selectCartShop(buyerId);
        String result="没有已下架或被冻结商品商品";
        for(Cart cart:cartList){
            if(cart.getWaresNumber()==0){
                cartMapper.deleteById(cart.getCartId());
                result="商品已从购物车中删除";
            }
        }
        return result;
    }
}
